package com.zz.bid.service;

import com.zz.bid.entity.Bid;
import com.zz.bid.exception.base.BidException;
import com.zz.bid.mapper.BidMapper;
import com.zz.bid.to.BidTO;
import com.zz.bid.to.ResponseWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chenkehan
 * created at 2020/4/4  6:26 下午
 */
@Service
@Slf4j
public class BidService {

    @Autowired
    private BidMapper bidMapper;

    public void create(BidTO bidTO) throws BidException {
        try {
            bidMapper.createBid(bidTO);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BidException(e.getMessage());
        }
    }


    public List<BidTO> search(BidTO bidTO) throws BidException {
        try {
            List<Bid> bidList = bidMapper.search(bidTO);
            if (CollectionUtils.isEmpty(bidList)) {
                return new ArrayList<>();
            }
            return bidList.stream().map(item -> BidTO.builder().id(item.getId()).name(item.getName()).openDate(item.getOpenDate()).build()).collect(Collectors.toList());
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BidException(e.getMessage());
        }
    }
}
